<template>
  <s-layout id="layout" :title="'我的拜访'" navbar="normal" leftIconColor="#333" :autoBack="false" :bgColor="state.bgColor"
    :tabbarshow='false' :titleStyle="{ color: '#333', fontSize: '32rpx' }">
    <view class="call_on" :style="{ paddingTop: (safeAreaInsets?.top + 44) + 'px' }">
      <scroll-view v-if="list.length" scroll-y class="comprehensive" @scrolltolower='listScrolltolower'
        show-scrollbar="true" :refresher-enabled="isOpenRefresh" :refresher-triggered="triggered"
        :refresher-threshold="100" refresher-background="white" @refresherpulling="onPulling"
        @refresherrefresh="onRefresh" @refresherrestore="onRestore" @refresherabort="onAbort" @scroll="onScroll">
        <view class="item_wrap">
          <view class="item" v-for="(item, index) in list" :key="index">
            <view class="item_left">
              <view class="title">{{ item.company }}</view>
              <view class="name_and_phone">
                <text class="name">{{ item.name }}</text>
                <up-text class="phone" mode="phone" :text="item.phone" format="encrypt" :color="'#333'"
                  :size="'23rpx'"></up-text>
              </view>
              <view class="addresss">
                <image :src="`${baseUrl}/uploads/uniapp_image/staff/weizhi.png`" mode=""></image>
                <text>{{ item.address }}</text>
              </view>
            </view>
            <view class="item_right">
              <text class="name" @click="lookDetails(item)">查看详情</text>
              <u-icon size="10" name="arrow-right" color="#0038F7"></u-icon>
            </view>
          </view>

        </view>
        <!-- 加载中 && 没有更多数据了 -->
        <view>
          <s-loadmore-gather :current_page="listPage" :last_page="listlast_page" ref="loadmoreRef"></s-loadmore-gather>
        </view>
      </scroll-view>
      <u-empty v-else mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png" text="暂无内容"
        marginTop="50%"></u-empty>
    </view>
    <!-- 底部提交按钮 -->
    <view>
      <s-button @netWork="submitForm" :text="text"></s-button>
    </view>
  </s-layout>
</template>

<script setup>
import { baseUrl } from '@/sheep/config';
import sheep from '@/sheep';
import { onLoad, onPullDownRefresh } from "@dcloudio/uni-app";
import { ref } from 'vue';
const { safeAreaInsets } = uni.getSystemInfoSync()
const state = ref({
  bgColor: '#fff'
})
const text = ref("新增拜访");
onLoad((options) => {
  listpagenumber.value = 1;
  list.value = []
  getVisitRecordList()

})

//数据请求
const list = ref([])
const listPage = ref()
const listlast_page = ref();
const listpagenumber = ref(1);

const getVisitRecordList = async () => {
  let params = {
    page: listpagenumber.value
  };
  const res = await sheep.$api.visit_record.getVisitRecordList(params);
  list.value = [...list.value, ...res?.data.rows];
  listPage.value = res.data.current_page
  listlast_page.value = res.data.last_page
}

const loadmoreRef = ref()
//滚动触底
const listScrolltolower = () => {
  loadmoreRef.value.PopupScrolltolower()
  if (listPage.value < listlast_page.value) {
    listpagenumber.value += 1 ;
  } else {
    // isOpenRefresh.value = false
    return;
  }
  getVisitRecordList();
};
//跳转页面
const submitForm = () => {
  sheep.$router.go('/subpackage/staff/workbench/call_on/call_on_add');
}
//查看详情
const lookDetails = (item) => {
  sheep.$router.go('/subpackage/staff/workbench/call_on/call_on_add', {
    id: item.id,
  });
}
// 页面下拉刷新
const freshing = ref(false)
const triggered = ref(false)
const isOpenRefresh = ref(true)  // 是否开启下拉
// 自定义下拉刷新控件被下拉
const onPulling = (e) => {
  if (e.detail.deltaY < 0) return  // 防止上滑页面也触发下拉
  triggered.value = true;
}
// 自定义下拉刷新被触发
const onRefresh = () => {
  if (freshing.value) return;
  freshing.value = true;
  setTimeout(() => {
    listpagenumber.value = 1;
    list.value = []
    triggered.value = false;
    freshing.value = false;
    getVisitRecordList();
  }, 500);
}
// 自定义下拉刷新被复位
const onRestore = () => {
  triggered.value = 'restore'; // 需要重置
  // console.log("onRestore");
}
// 自定义下拉刷新被中止
const onAbort = () => {
  // console.log("onAbort");
}
</script>

<style lang="scss" scoped>
// 滚动
.comprehensive {
  width: calc(100% - 40rpx);
  background-color: rgba(0, 0, 0, 0);
  margin: 0 20rpx;
  flex: 1;
  height: 100%;
  box-sizing: border-box;
}
//列表
.call_on {
  width: 100%;
  height: 80vh;
  .item_wrap {
    margin-top: 20rpx;
    .item {
      margin-bottom: 20rpx;
      border-radius: 16rpx;
      background-color: #FFFFFF;
      box-shadow: 0 4rpx 8rpx 0 rgba(0, 0, 0, 0.05);
      display: flex;
      justify-content: space-around;
      align-items: center;
      .item_left {
        width: 70%;
        padding: 30rpx 0 0 30rpx;
        height: 168rpx;
        flex-direction: column;
        font-family: PingFangSC;
        color: $uni-text-color;
        .title {
          font-weight: $uni-font-weight;
          font-size: $uni-font-size-base;
          margin-bottom: 10rpx;
        }
        .name_and_phone {
          .name {
            margin-right: 8rpx;
          }
          display: flex;
          align-items: center;
          color: #666666;
          font-weight: 400;
          font-size:26rpx;
          margin-bottom: 20rpx;
        }
        .addresss {
          image {
            width: 18rpx;
            height: 26rpx;
            margin-right: 8rpx;
          }
          display: flex;
          align-items: center;
          color: #999999;
          font-weight: 500;
          font-size: $uni-font-size-sm;
        }
      }
      .item_right {
        padding-right: 30rpx;
        font-family: PingFangSC;
        color: #0038F7;
        font-weight: 400;
        font-size: 22rpx;
        display: flex;
        .name {
          margin-right: 6rpx;
        }
      }
    }
  }
}
.loading-text {
  text-align: center;
  font-size: $uni-font-size-base;
  color: #666;
  padding: 20rpx 0 50rpx;
}
</style>